%include "&studyroot.\Work\Programs\Macros\univariate1a.sas";
%macro prnt(program=,disname=, idisname=, ds_list=, where= , title= );

proc format;
value treat
  1 = "Treatment A"
  2 = "Treatment B"
  3 = "Treatment C"
  4 = "Overall"
  ;
quit;

proc freq data=adam.adlb;
table parcat1*param;
run;

data temporary ;
   length subjid $10.; 
   set adam.adlb(where=(&where));
   if upcase(saffl)='Y' and (indexc(upcase(actarmcd),'ABC') or upcase(ablfl)='Y');
/*   subjid = strip(substr(usubjid,13)); */
/*   if SAFFL = 'Y' and visityp="Scheduled" ;*/
   param = tranwrd(param, "10e3", "10^{super 3}");
   param = tranwrd(param, "10e6", "10^{super 6}");
   param = tranwrd(param, "10e9", "10^{super 9}");
   param = tranwrd(param, "10e12", "10^{super 12}");
   param = tranwrd(param, "10^3", "10^{super 3}");
   param = tranwrd(param, "10^6", "10^{super 6}");
   param = tranwrd(param, "10^9", "10^{super 9}");
   param = tranwrd(param, "10^12", "10^{super 12}");
 
/*	param = tranwrd(param, "10^3", "10^{super 3}");*/
/*	param = tranwrd(param, "10^6", "10^{super 6}");*/
/*	param = tranwrd(param, "10^9", "10^{super 9}");*/
/*	param = tranwrd(param, "10^12", "10^{super 12}");*/
   param = tranwrd(param, "-"||trim(parcat1), "");
   tocount=1;
   /*avisit=tranwrd(avisit,"Screening Day -21 to -2","Screening");
    avisit=tranwrd(avisit,"End of Study or Early Termination","EOS/ET"); */
   /*if avisit in ("P1 Day -1" "P1 Day 7" "P2 Day 14" "Post Study Day 21" "End of Study Day 45") then output;*/
   *if paramcd in ("CHOL" "CHOLHDL" "LDL") then delete;

   	/*column=trtan;
      trtp=put(column, treat.);
      output; 
      column=4;
      trtp=put(column, treat.);
      output; */
   
   trtp='Overall';
   TRTPN=4; 
   column=4;

run;

%macro get_deci(dom=,lib=work,in=,id=paramcd,var=avalc,debug=0);
%if %length(&lib)=0 %then %let lib=work;
%if %length(&debug)=0 %then %let debug=0;
%else %if &debug^=1 %then %let debug=0;
%let dsid=%sysfunc(open(&lib..&in));
%let dsid=%sysfunc(close(&dsid));
proc sql;
create table gd_val as
   select distinct  parcat1, &id,&var
   from &lib..&in where %ifnum(&var) and index(&var,".")
   order by parcat1, &id;
create table gd_deci as
   select distinct parcat1, &id,
   max(length(scan(&var,2,"."))) as deci
   from gd_val group by parcat1,&id;
quit;
%if &debug=0 %then %do; %*clrw(gd_val); %end;
%mend get_deci;

%get_deci(dom=temporary,lib=work,in=temporary);

proc sort data=temporary;
by parcat1 paramcd;
run;

data temporary;
merge temporary(in=a) gd_deci;
by parcat1 paramcd;
if a;
 if missing(deci) then nbdec=0;
   else nbdec=deci;
run;

data pop; 
  set adam.adsl(where=(saffl='Y')); 

  by usubjid;
  
/*  sexc=upcase(sexc);*/
  tocount=1; 
   
   trtp='Overall';
   TRTPN=4; 
   column=4;
  
run;


proc freq data=temporary;
tables column / list missing;
run;

Proc freq data = temporary;
   tables param/ list missing nocum nopercent;
run;

proc sort data=temporary;
  where column ne . and paramn ne .;
  by paramn paramcd param avisitn avisit column;
run;

Proc SQL;
  create table want as
     select distinct parcat1n, PARCAT1, param,paramn,nbdec 
   FROM temporary
   where aval ne .
   order parcat1n ,paramn
   ;
QUIT;
 

proc sort data=pop nodupkeys;
  by subjid column;
run;

Proc sort data =pop out = spopone nodupkey ;
   by column trtp;
run;

data tofmt;
   set spopone;
run;

%makefmt(tofmt, column ,trtp, _ftrt);


*******************************************************************************************************
Define population for each dose group
******************************************************************************************************;
%popu(datain=pop);

%macro a;
%do i = 1 %to &maxtrt;
   %put pop&i=&&&pop&i;
%end;
%mend a; 

%a;


*******************************************************************************************************
Define headers for each columns
******************************************************************************************************;
%heads(fmt=_ftrt);

*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "n"
                2 = "Mean (SD)"
                3 = "Median"
                4 = "Min, Max"
;
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;
data complet;
run;

%macro dolab(paramn, nbdec);

%univariate5(var=aval,
             in=temporary,
             out=_tmpa&paramn.,
             width=6,
             decimal=&nbdec, 
             level1=1,
             level2=,
             level3=,
             by=parcat1n PARCAT1 paramn paramcd param avisitn avisit ,
             wh=paramn = &paramn. and aval ne . and avisit ne ''); 


data _tmpa&paramn;
set _tmpa&paramn;
length level1_label value $200;
level1_label = put(level2, level1l.);
value="Value";
type = 1; 
cat = 1; 
if level2=5 then delete;
run;

%univariate5(var=chg,
             in=temporary,
             out=_tmpb&paramn.,
             width=6,
             decimal =&nbdec ,
             level1=1,
             level2=,
             level3=,
             by=parcat1n PARCAT1 paramn paramcd param avisitn avisit ,
             wh=paramn = &paramn. and chg ne . and avisit ne ''); 

data _tmpb&paramn;
set _tmpb&paramn;
length level1_label value $200;
level1_label = put(level2, level1l.);
value="Change from Baseline";
type = 2; 
cat = 2; 
if level2=5 then delete;
run;

data complet;
set complet _tmpa&paramn _tmpb&paramn;
run;

%mend dolab;


%global   paramn  nbdec ;
%macro a;
%let fp = %sysfunc(open(want, i)); /* create a file pointer for the param dataset*/
%syscall set(fp);
%do %until (&rc ne 0);
   /*extract variable value and put them in grobal macro variable of the same name as in the dataset*/
   %let rc = %sysfunc(fetch(&fp));
   /* the value of the rc is 0 if the fetch command was poperly executed */
   %if &rc = 0 %then
      %do;
		
         %dolab( &paramn, &nbdec);
      %end;
   %end;
%let rc = %sysfunc(close(&fp)); 

%mend a;

%a;
 
data all;
set complet;
desc=put(level1,level1l.);
if param=" " then delete;
if level1=4 then do;
col4="    "||strip(col4);
end;
if value = 'Value'; 
run;

proc sql noprint;
select count(distinct paramn) into :nobs from all;
quit;

%let n=%sysfunc(compress(&nobs));
%put &nobs;

/*
%page_cut(in=all,sort=parcat1n PARCAT1 paramn param avisitn avisit level2,nocut=avisit,newpage=paramn,pagesize=30,skip=avisit,
order=,columns=,width=,defln=1,out=final); 

proc sort data=final  out=repport;
  by page parcat1n PARCAT1 paramn param avisitn avisit level2;
run;
*/

%page_cut(in=all,sort=parcat1n PARCAT1 param avisitn avisit cat value type level1 level1_label /*col1 col2 col3*/ col4,
nocut=avisit,newpage=/*param*/,pagesize=32,skip=avisit,
order=parcat1n PARCAT1 paramn param avisitn avisit  cat value type level1 level1_label,
columns=param avisit value level1_label /*col1 col2 col3*/ col4,
width=2.0 2.0 2.0 1.0 3 1.25 1.25 1.25,defln=1,out=final);

proc sort data=final  out=report;
  by page param avisitn avisit cat value type level1 ;
run;

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;

title3 " ";
title4 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list., program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..LB.rtf";

%open_rtf;
%empty_ds_fix(report);

title5 "Summary of &title";
title6 "(Safety Population) ";


proc report data=report nowd split='~' missing; 
 column page parcat1n PARCAT1 /*paramn*/ param avisitn avisit cat value type level2 level1_label 
/*col1 col2 col3 */col4;
 define page         / " " order order=internal noprint;
 define parcat1n     / " " order order=internal noprint;
 define PARCAT1      / " " order order=internal noprint;
 define param        / "Parameter (unit)" order order=internal style(column)=[cellwidth=&__param.in just=left] style(header)=[just=left];
 define avisitn      / " " order order=internal noprint;
 define avisit       / "Visit" order order=internal style(column)=[cellwidth=&__avisit.in just=left] style(header)=[just=left];
 define cat          / " " order order=internal noprint;
 define value        / "" order order=internal style(column)=[cellwidth=&__value.in just=left] style(header)=[just=left];
 define type         / "" order order=internal noprint;
 define level2       / "" order order=internal noprint;
 define level1_label / "" order order=internal style(column)=[cellwidth=0.8in just=left] style(header)=[just=left];
 *define col1         / " &header1" style(column)=[cellwidth=&__col1.in just=c] style(header)=[just=c];
 *define col2         / " &header2" style(column)=[cellwidth=&__col1.in just=c] style(header)=[just=c];
 *define col3         / " &header3" style(column)=[cellwidth=&__col1.in just=c] style(header)=[just=c];
 define col4         / "&header4" style(column)=[cellwidth=&__col4.in just=c] style(header)=[just=c];
 
 break after page   / page;
 %if &nb_obs GT 0 %then %do;
 compute before value / style=[cellwidth=6in just=left];
  line " ";
 endcomp;
 %end;
 %if &nb_obs EQ 0 %then %do;
   compute after page/style=[cellwidth=9.7in just=left];
    line "No Lab Values Were Measured.";
   endcomp;
 %end;

run;

run;

%close_rtf;
%arrange_rtf;
%mend prnt;

%prnt(program=T14_03_04_0X_SUM_LB, disname=14.3.4.1, idisname=14.3.4.1, ds_list=ADLB, where=%str(parcat1="CHEMISTRY" and anl01fl = 'Y'), title=%str(General Biochemistry));

%clrw;

%prnt(program=T14_03_04_0X_SUM_LB, disname=14.3.4.2, idisname=14.3.4.2, ds_list=ADLB, where=%str(parcat1="HEMATOLOGY" and anl01fl = 'Y'), title=%str(Hematology));
%clrw;

%prnt(program=T14_03_04_0X_SUM_LB, disname=14.3.4.3, idisname=14.3.4.3, ds_list=ADLB, where=%str(parcat1="URINALYSIS" and anl01fl = 'Y'), title=%str(Urinalysis));
%clrw;

